package com.dhcc.sdc.task;

import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.sdc.base.dao.SdcBaseAssessmenentryMapper;
import com.dhcc.sdc.base.dao.SdcBaseAssessmentMapper;
import com.dhcc.sdc.base.entity.SdcBaseAssessmenentryEntity;
import com.dhcc.sdc.base.entity.SdcBaseAssessmentEntity;
import com.dhcc.sdc.busi.dao.SdcBusiEvaluationHisMapper;
import com.dhcc.sdc.busi.entity.SdcBusiEvaluationEntity;
import com.dhcc.sdc.busi.entity.SdcBusiEvaluationHisEntity;
import com.dhcc.sdc.busi.entity.SdcBusiEvaluationItemEntity;
import com.dhcc.sdc.busi.entity.SdcBusiQcMrlistEntity;
import com.dhcc.sdc.busi.function.GetSdcValueSrv;
import com.dhcc.sdc.busi.service.ISdcBusiQcMrlistService;
import com.dhcc.sds.busi.entity.SdsQcincaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dhcc.sds.busi.service.ISdsQcincaseService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description:评估表实现系统自动评估功能
 * @ClassName:  AutoAssessmentTask
 * @date:2023年3月28日 下午3:45:16
 * @author: zhangdc
 */
@Service("autoAssessmentTask")
public class AutoAssessmentTask {
    @Autowired
    private ICdcDictionaryService cdcDictionaryService;
    @Autowired
    private ISdcBusiQcMrlistService sdcBusiQcMrlistService;
    @Resource
    private SdcBaseAssessmenentryMapper sdcBaseAssessmenentryMapper;
    @Resource
    private SdcBaseAssessmentMapper sdcBaseAssessmentMapper;
    @Autowired
    private GetSdcValueSrv getSdcValueSrv;
    @Resource
    private SdcBusiEvaluationHisMapper sdcBusiEvaluationHisMapper;

    public void autoTaskForAssessment(){
        //就诊状态:在院
        //CdcDictionaryEntity admStatus = cdcDictionaryService.queryByTypeCodeAndCode("PatData", "bus_adm_status", "A");
        //查询入组在院患者
        //List<SdsQcincaseEntity> sdsQcincaseEntities = sdsQcincaseService.selectContraindicationByAdmStatus(admStatus.getId());
        SdcBusiQcMrlistEntity sdcBusiQcMrlistEntity1 = sdcBusiQcMrlistService.selectById(1638354591662530562l);
        List<SdcBusiQcMrlistEntity> sdcBusiQcMrlistEntities = Arrays.asList(sdcBusiQcMrlistEntity1);
        a:for (SdcBusiQcMrlistEntity sdcBusiQcMrlistEntity : sdcBusiQcMrlistEntities){
            List<SdcBaseAssessmenentryEntity> sdcBaseAssessmenentryEntities = sdcBaseAssessmenentryMapper.selectByEntityId(sdcBusiQcMrlistEntity.getEntityId());
            b:for (SdcBaseAssessmenentryEntity sdcBaseAssessmenentry : sdcBaseAssessmenentryEntities){
                //计算公式
                String formula = sdcBaseAssessmenentry.getFormula();
                if (!StringUtils.isEmpty(formula)){
                    //todo:测试规则，后面改成配置规则库
                    if (formula.contains("age")){
                        int age1 = Integer.parseInt(formula.replaceAll("[^0-9]", ""));
                        String sign = formula.replaceAll("[0-9]", "");
                        String res = getSdcValueSrv.BaseInfoData(sdcBusiQcMrlistEntity.getEpisodeId(), "", "age").toString();
                        Integer age2 = Integer.parseInt(res);
                        if (sign.equals("age>=") && age2 >= age1){
                            updateBusiEvaluation(sdcBaseAssessmenentry.getId(), sdcBusiQcMrlistEntity.getEpisodeId(), res, "就诊信息-年龄:" + res, 1);
                        }
                    }else if (formula.contains("temperature")){
                        //temperature≤37.8
                        String res = getSdcValueSrv.NurInfo(sdcBusiQcMrlistEntity.getEpisodeId(), "", "temperature").toString();
                        BigDecimal a = new BigDecimal(res);
                        BigDecimal b = new BigDecimal("37.8");
                        int compareResult = a.compareTo(b);
                        if (compareResult < 0 || compareResult == 0){
                            updateBusiEvaluation(sdcBaseAssessmenentry.getId(), sdcBusiQcMrlistEntity.getEpisodeId(), res, "", 1);
                        }
                    }else if (formula.contains("heartbeat")){
                        //heartbeat≤100
                        String res = getSdcValueSrv.NurInfo(sdcBusiQcMrlistEntity.getEpisodeId(), "", "heartbeat").toString();
                        BigDecimal a = new BigDecimal(res);
                        BigDecimal b = new BigDecimal("100");
                        int compareResult = a.compareTo(b);
                        if (compareResult <= 0 ){
                            updateBusiEvaluation(sdcBaseAssessmenentry.getId(), sdcBusiQcMrlistEntity.getEpisodeId(), res, "", 1);
                        }
                    }else if (formula.contains("sysPressure")){
                        //sysPressure≥90mmHg
                        String res = getSdcValueSrv.NurInfo(sdcBusiQcMrlistEntity.getEpisodeId(), "", "sysPressure").toString();
                        BigDecimal a = new BigDecimal(res);
                        BigDecimal b = new BigDecimal("90");
                        int compareResult = a.compareTo(b);
                        if (compareResult >= 0){
                            updateBusiEvaluation(sdcBaseAssessmenentry.getId(), sdcBusiQcMrlistEntity.getEpisodeId(), res, "", 1);
                        }
                    }else if (formula.contains("DiagInfo")){
                        String res = getSdcValueSrv.DiagInfo(sdcBusiQcMrlistEntity.getEpisodeId(), "呼吸衰竭", "Quantity").toString();
                        Integer quantity = Integer.parseInt(res);
                        if (quantity > 0){
                            updateBusiEvaluation(sdcBaseAssessmenentry.getId(), sdcBusiQcMrlistEntity.getEpisodeId(), res, "", 1);
                        }
                    }
                }
            }
        }
    }

    private void updateBusiEvaluation(Long assessmenentryId, Long episodeId, String res, String keyWordsfrom, Integer auto){
        SdcBaseAssessmentEntity sdcBaseAssessmentEntity = sdcBaseAssessmentMapper.selectByAssessmenentryId(assessmenentryId);
        SdcBusiEvaluationHisEntity sdcBusiEvaluationHisEntity = new SdcBusiEvaluationHisEntity();
        sdcBusiEvaluationHisEntity.setEpisodeId(episodeId);
        sdcBusiEvaluationHisEntity.setAssessmentEntryId(assessmenentryId);
        sdcBusiEvaluationHisEntity.setAssessmentId(sdcBaseAssessmentEntity.getId());
        sdcBusiEvaluationHisEntity.setAuto(auto);
        sdcBusiEvaluationHisEntity = sdcBusiEvaluationHisMapper.selectOne(sdcBusiEvaluationHisEntity);
        if (null == sdcBusiEvaluationHisEntity){
            //insert
            sdcBusiEvaluationHisEntity = new SdcBusiEvaluationHisEntity();
            sdcBusiEvaluationHisEntity.setEpisodeId(episodeId);
            sdcBusiEvaluationHisEntity.setAssessmentEntryId(assessmenentryId);
            sdcBusiEvaluationHisEntity.setResult(res);
            sdcBusiEvaluationHisEntity.setEvalDate(new Date());
            sdcBusiEvaluationHisEntity.setAssessmentId(sdcBaseAssessmentEntity.getId());
            sdcBusiEvaluationHisEntity.setKeyWordsfrom(keyWordsfrom);
            sdcBusiEvaluationHisEntity.setAuto(auto);
            sdcBusiEvaluationHisEntity.insert();
        }else {
            //update
            sdcBusiEvaluationHisEntity.setResult(res);
            sdcBusiEvaluationHisEntity.setKeyWordsfrom(keyWordsfrom);
            sdcBusiEvaluationHisEntity.setEvalDate(new Date());
            sdcBusiEvaluationHisEntity.updateById();
        }
    }

}
